package de.gof.ppstool.fertigungsverwaltung.ui;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.faces.FacesException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;

@SuppressWarnings("unchecked")
@ManagedBean(name = StuecklistenTreeControllerBean.NAME)
@SessionScoped
public class StuecklistenTreeControllerBean implements Serializable {
	public StuecklistenTreeControllerBean() {
		super();
	}

	private static final long serialVersionUID = -5826443178713257036L;
	public static final String NAME = "simpleTreeBean";
	private TreeNode rootNode3 = null;
	private TreeNode rootNode1 = null;
	private TreeNode rootNode2 = null;
	private List<String> selectedNodeChildren = new ArrayList<String>();
	private List<String> selectedNodeChildren1 = new ArrayList<String>();
	private List<String> selectedNodeChildren2 = new ArrayList<String>();
	private String nodeTitle;
	private static final String DATA_PATH_KINDER = "/lagerverwaltung/tree_kinder.properties";
	private static final String DATA_PATH_DAMEN = "/lagerverwaltung/tree_damen.properties";
	private static final String DATA_PATH_HERREN = "/lagerverwaltung/tree_herren.properties";

	private void addNodes(String path, TreeNode node, Properties properties) {
		boolean end = false;
		int counter = 1;
		while (!end) {
			String key = path != null ? path + '.' + counter : String
					.valueOf(counter);
			String value = properties.getProperty(key);
			if (value != null) {
				TreeNodeImpl nodeImpl = new TreeNodeImpl();
				nodeImpl.setData(value);
				node.addChild(new Integer(counter), nodeImpl);
				addNodes(key, nodeImpl, properties);
				counter++;
			} else {
				end = true;
			}

		}
	}

	private void loadTree3() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		InputStream dataStream = externalContext
				.getResourceAsStream(DATA_PATH_KINDER);
		try {
			Properties properties = new Properties();
			properties.load(dataStream);
			rootNode3 = new TreeNodeImpl();
			addNodes(null, rootNode3, properties);
		} catch (IOException e) {
			throw new FacesException(e.getMessage(), e);
		} finally {
			if (dataStream != null) {
				try {
					dataStream.close();
				} catch (IOException e) {
					externalContext.log(e.getMessage(), e);
				}

			}

		}

	}

	public void processSelection3(NodeSelectedEvent event) {
		HtmlTree tree = (HtmlTree) event.getComponent();
		nodeTitle = (String) tree.getRowData();
		selectedNodeChildren.clear();
		TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
		if (currentNode.isLeaf()) {
			selectedNodeChildren.add((String) currentNode.getData());
		} else {
			Iterator<Map.Entry<Object, TreeNode>> it = currentNode
					.getChildren();
			while (it != null && it.hasNext()) {
				Map.Entry<Object, TreeNode> entry = it.next();
				selectedNodeChildren.add(entry.getValue().getData().toString());
			}
		}

	}

	private void loadTree1() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		InputStream dataStream = externalContext
				.getResourceAsStream(DATA_PATH_DAMEN);
		try {
			Properties properties = new Properties();
			properties.load(dataStream);
			rootNode1 = new TreeNodeImpl();
			addNodes(null, rootNode1, properties);
		} catch (IOException e) {
			throw new FacesException(e.getMessage(), e);
		} finally {
			if (dataStream != null) {
				try {
					dataStream.close();
				} catch (IOException e) {
					externalContext.log(e.getMessage(), e);
				}

			}

		}

	}

	public void processSelection1(NodeSelectedEvent event) {
		HtmlTree tree = (HtmlTree) event.getComponent();
		nodeTitle = (String) tree.getRowData();
		selectedNodeChildren1.clear();
		TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
		if (currentNode.isLeaf()) {
			selectedNodeChildren1.add((String) currentNode.getData());
		} else {
			Iterator<Map.Entry<Object, TreeNode>> it = currentNode
					.getChildren();
			while (it != null && it.hasNext()) {
				Map.Entry<Object, TreeNode> entry = it.next();
				selectedNodeChildren1
						.add(entry.getValue().getData().toString());
			}
		}

	}

	private void loadTree2() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		InputStream dataStream = externalContext
				.getResourceAsStream(DATA_PATH_HERREN);
		try {
			Properties properties = new Properties();
			properties.load(dataStream);
			rootNode2 = new TreeNodeImpl();
			addNodes(null, rootNode2, properties);
		} catch (IOException e) {
			throw new FacesException(e.getMessage(), e);
		} finally {
			if (dataStream != null) {
				try {
					dataStream.close();
				} catch (IOException e) {
					externalContext.log(e.getMessage(), e);
				}

			}

		}

	}

	public void processSelection2(NodeSelectedEvent event) {
		HtmlTree tree = (HtmlTree) event.getComponent();
		nodeTitle = (String) tree.getRowData();
		selectedNodeChildren2.clear();
		TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
		if (currentNode.isLeaf()) {
			selectedNodeChildren2.add((String) currentNode.getData());
		} else {
			Iterator<Map.Entry<Object, TreeNode>> it = currentNode
					.getChildren();
			while (it != null && it.hasNext()) {
				Map.Entry<Object, TreeNode> entry = it.next();
				selectedNodeChildren2
						.add(entry.getValue().getData().toString());
			}
		}

	}

	public TreeNode getTreeNode3() {
		if (rootNode3 == null) {
			loadTree3();
		}

		final FacesContext ctx = FacesContext.getCurrentInstance();
		final String path = ctx.getViewRoot().getViewId();
		if (!path.equals("/lagerverwaltung/structurelist.xhtml")) {
			return null;
		}

		return rootNode3;
	}

	public TreeNode getTreeNode1() {
		if (rootNode1 == null) {
			loadTree1();
		}

		final FacesContext ctx = FacesContext.getCurrentInstance();
		final String path = ctx.getViewRoot().getViewId();
		if (!path.equals("/lagerverwaltung/structurelist.xhtml")) {
			return null;
		}

		return rootNode1;
	}

	public TreeNode getTreeNode2() {
		if (rootNode2 == null) {
			loadTree2();
		}

		final FacesContext ctx = FacesContext.getCurrentInstance();
		final String path = ctx.getViewRoot().getViewId();
		if (!path.equals("/lagerverwaltung/structurelist.xhtml")) {
			return null;
		}

		return rootNode2;
	}

	public String getNodeTitle() {
		return nodeTitle;
	}

	public void setNodeTitle(String nodeTitle) {
		this.nodeTitle = nodeTitle;
	}

}
